# -*- coding: UTF-8 -*-
import paramiko
from scp import SCPClient
import ConfigParser

#通过ip和密码将文件发送到远端服务器上。如果有密钥，密码错误也能下发。
def module_tool_push_file(hostname, password, localfile, remotefile, username='root', port=22):
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        ssh.connect(hostname=hostname, password=password, port=port, username=username)
        scpclient = SCPClient(ssh.get_transport(), socket_timeout=15.0)
        remotepath = remotefile
        localpath = localfile
        scpclient.put(localpath, remotepath)
        return 0
    except Exception,e:
        print e
        return 1
    finally:
        ssh.close()

#获取配置文件
def module_tool_get_conf(section, name):
    conf = ConfigParser.ConfigParser()
    conf.read("software.ini")
    return conf.get(section, name)